package com.bdqn.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.base.R;
import com.bdqn.base.logger.WebLogger;
import com.bdqn.pojo.ShopComment;
import com.bdqn.service.ShopCommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 评论
 *
 * @author lilibo
 * @date 2021/10/09
 */
@Api(tags="评论接口")
@RestController
@RequestMapping("/shopcomment")
public class ShopCommentController {

    @Autowired
    private ShopCommentService shopCommentService;

    /**
     * 新增评论记录
     *
     * @param shopComment
     * @return R
     */
    @ApiOperation("新增评论记录")
    @WebLogger("新增评论记录")
    @PostMapping("/add")
    public R save(@ApiParam("评论对象") @RequestBody ShopComment shopComment) {
        return new R<>(shopCommentService.save(shopComment));
    }

    /**
     * 修改评论记录
     *
     * @param shopComment
     * @return R
     */
    @ApiOperation("修改评论记录")
    @WebLogger("修改评论记录")
    @PostMapping("/update")
    public R update(@ApiParam("评论对象") @RequestBody ShopComment shopComment) {
        return new R<>(shopCommentService.updateById(shopComment));
    }

    /**
     * 通过id删除一条评论记录
     *
     * @param id
     * @return R
     */
    @ApiOperation("通过id删除一条评论记录")
    @WebLogger("通过id删除一条评论记录")
    @PostMapping("/delete")
    public R removeById(@ApiParam("评论id") Integer id) {
        return new R<>(shopCommentService.removeById(id));
    }

    /**
     * 通过id查询单条评论记录
     *
     * @param id
     * @return R
     */
    @ApiOperation("通过id查询单条评论记录")
    @WebLogger("通过id查询单条评论记录")
    @GetMapping("/{id}")
    public R<ShopComment> getById(@ApiParam("评论id") @PathVariable Integer id) {
        return new R<>(shopCommentService.getById(id));
    }

    /**
     * 通过评论信息查询评论记录
     *
     * @param shopComment 评论信息对象
     * @return R
     */
    @ApiOperation("通过评论信息查询评论记录")
    @WebLogger("通过评论信息查询评论记录")
    @GetMapping("/list")
    public R<List<ShopComment>> getList(@ApiParam("查询的评论对象") ShopComment shopComment) {
        return new R<>(shopCommentService.getList(shopComment));
    }

    /**
     * 分页查询评论
     *
     * @param pageNo 当前页
     * @param pageSize 每页条数
     * @param shopComment 评论
     * @return
     */
    @ApiOperation("分页查询评论")
    @WebLogger("分页查询评论")
    @GetMapping("/page")
    public R<IPage<ShopComment>> getPage(@ApiParam("当前页") @RequestParam(required = false, defaultValue = "1")  Long pageNo, @ApiParam("每页条数") @RequestParam(required = false, defaultValue = "10") Long pageSize, @ApiParam("查询的评论对象") ShopComment shopComment) {
        return new R<>(shopCommentService.getPage(new Page<>(pageNo, pageSize), shopComment));
    }

    /**
     * 分页查询评论数据
     *
     * @param pageNo 当前页
     * @param pageSize 每页条数
     * @param shopComment 评论
     * @return 返回对象中的count为数据总量
     */
    @ApiOperation("分页查询评论数据")
    @WebLogger("分页查询评论数据")
    @GetMapping("/pagelist")
    public R<List<ShopComment>> getPageList(@ApiParam("当前页") @RequestParam(required = false, defaultValue = "1")  Long pageNo, @ApiParam("每页条数") @RequestParam(required = false, defaultValue = "10") Long pageSize, @ApiParam("查询的评论对象") ShopComment shopComment) {
        IPage<ShopComment> page = shopCommentService.getPage(new Page<>(pageNo, pageSize), shopComment);
        return new R<>(page.getRecords(), page.getTotal());
    }

}
